package com.day1;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import com.alibaba.fastjson.TypeReference;

import java.util.List;

public class Json {
    public static void main(String[] args) {
        User[] arr = new User[2];

        Car car = new Car();
        car.setBrand("宝马");
        car.setPrice(350000);

        Car car1 = new Car();
        car1.setBrand("奔驰");
        car1.setPrice(400000);

        User user = new User();
        user.setId(1);
        user.setName("小马");
        user.setAge(12);
        user.setCar(car);


        User user1 = new User();
        user1.setId(2);
        user1.setName("小牛");
        user1.setAge(13);
        user1.setCar(car1);

        arr[0] = user;
        arr[1] = user1;

 //       {
//            "id":1,
//            "name":"小梅",
//            "age"：1,
//            "car":{
//                "brand":"宝马",
//                 "price":1000
//            }


            String str = JSON.toJSONString(user);
            System.out.println(str);

            User jsonObject = JSON.parseObject(str,User.class);
            System.out.println(jsonObject);

        String arrstr = JSONArray.toJSONString(arr);

        System.out.println(arrstr);

        List<User> users = JSONArray.parseArray(arrstr, User.class);
        System.out.println(users);
        List<User[]> users1 = JSONArray.parseArray(arrstr, User[].class);
        System.out.println(users1);

        List<User> users2 = JSON.parseObject(arrstr, new TypeReference<List<User>>(){});
        System.out.println(users2);

    }

    }